package com.yingxin.yxerp.module.attachment.controller;

import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.constant.PathConst;
import com.yingxin.yx.framework.commons.dto.AttachmentDto;
import com.yingxin.yx.framework.commons.dto.AttachmentQueryDto;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.helper.PropertiesHelper;
import com.yingxin.yxerp.module.attachment.entity.Attachment;
import com.yingxin.yxerp.module.attachment.mapper.AttachmentMapper;
import com.yingxin.yxerp.module.attachment.service.IAttachmentService;
import com.yingxin.yxerp.module.attachment.utils.ReadTextFromFileRegion;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 附件表 前端控制器
 * @author zhuchangjie
 * @since 2022-01-23
 */

@RestController
@RequestMapping("/attachment")
public class AttachmentController {

    @Autowired
    private IAttachmentService attachmentService;

    @SuppressWarnings("all")
    @Autowired
    private AttachmentMapper attachmentMapper;

    @ApiOperation(value = "上传单个附件（新增和修改页面临时上传,返回附件id）")
    @PostMapping("/temp/upload")
    public AttachmentDto upload(@RequestParam("file") MultipartFile file,
                                @RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "businessId", required = false) String businessId,
                                @RequestParam(name = "businessType", required = false) String businessType,
                                @RequestParam(name = "businessAttr", required = false) String businessAttr,
                                @RequestParam(name = "fileScanning", required = false) String fileScanning) {
        Attachment attachment = attachmentService.upload(file, name, businessId, businessType, businessAttr);
        AttachmentDto attachmentDto = new AttachmentDto(attachment.getId(), attachment.getFileName(), attachment.getPath());
        // 判断附件是否扫描文件内容
        if (CommonConst.TRUE.equals(fileScanning)){
            if (attachmentDto.getAttachmentPath().contains(".pdf")){
                // 上传文件位置
                String url = PropertiesHelper.getInstance().getProperty(PathConst.UPLOAD_DIR);
                try {
                    Map<String, String> map = ReadTextFromFileRegion.doPdfInvoice(new File(url+attachmentDto.getAttachmentPath()));
                    attachmentDto.setInvoiceMap(map);
                }catch (Exception e){}
            }
        }
        return attachmentDto;
    }

    @ApiOperation(value = "删除附件")
    @PostMapping("/del")
    public void delete(@RequestParam(name = "businessId") String businessId,
                       @RequestParam(name = "businessType") String businessType,
                       @RequestParam(name = "businessAttr", required = false) String businessAttr) {
        attachmentService.deleteByBiz(businessId, BusinessType.getEnum(businessType), businessAttr);
    }

    @ApiOperation(value = "查询附件 by biz")
    @PostMapping("/biz/query")
    public List<AttachmentDto> get(@RequestBody AttachmentQueryDto dto) {
        return attachmentService.listByBiz(dto.getBusinessId(), BusinessType.getEnum(dto.getBusinessType()), dto.getBusinessAttr());
    }

}